package com.attedu.jdbc;

import com.attedu.jdbc.util.DBUtil;
import com.mysql.cj.jdbc.exceptions.PacketTooBigException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author tune_the last
 * @date 2025/11/4 - 10:26
 */
public class Demo05Login {
    public static void main(String[] args) {
        try(Connection connection = DBUtil.getConnection()) {
            String sql = "SELECT nickname FROM user WHERE username = ? AND PASSWORD = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            Scanner in = new Scanner(System.in);
            System.out.print("请输入用户名：");
            preparedStatement.setString(1, in.next());
            System.out.print("请输入密码：");
            preparedStatement.setString(2, in.next());
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                String nickname = resultSet.getString("nickname");
                System.out.println("恭喜您 " + nickname +" 登录成功");
            }else {
                System.out.println("用户名和密码错误");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
